/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Manejadores;

import Gestion.Boletin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
/**
 *
 * @author Usuario
 */
public class ControlBoletin {
static Connection con;
static ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();

public static Boletin consultarBoletin(int cod) throws SQLException, Exception {
    mbd.conectar();
    con=mbd.getConexion();
     String codBol=cod +"";
      if (codBol==null) {
          throw new SQLException("No hay elemento clave de la clase Boletin");
     }
         ResultSet rs = null;
         PreparedStatement pst = null;
         Boletin boletin = null;
     try {
         pst = con.prepareStatement("select * from Boletin where codBoletin = ?");
         pst.setInt(1,cod );

         rs = pst.executeQuery();
         while(rs.next()) {

             boletin = Boletin.load(rs);
         }
    } finally {
         if (rs != null) {
            rs.close();
         }
         if (pst != null) {
           pst.close();
         }
          return boletin;
    }
}
public static boolean existeBoletin(int cod) throws SQLException, Exception {
    mbd.conectar();
    con=mbd.getConexion();
     String codBol=cod +"";
      if (codBol==null) {
          throw new SQLException("No hay elemento clave de la clase Boletin");
     }
         ResultSet rs = null;
         PreparedStatement pst = null;
         boolean Boletin = false;
     try {
         pst = con.prepareStatement("select * from Boletin where codBoletin = ?");
         pst.setInt(1, cod);

         rs = pst.executeQuery();
         while(rs.next()) {

             Boletin = true;
         }
    } finally {
         if (rs != null) {
            rs.close();
         }
         if (pst != null) {
           pst.close();
         }
          return Boletin;
    }
}

public static void registrarBoletin(Boletin boletin) throws SQLException, Exception {
    mbd.conectar();
    con=mbd.getConexion();
     if (con == null ) {
          throw new SQLException("no hay conexion" );
     }
     PreparedStatement pst = null;//hacer nula la consulata.....
     try {
          pst = con.prepareStatement("Insert Into Boletin values(?,?,?,?)");
          pst.setInt(1,boletin.getCodigo());
          pst.setString(2,boletin.getFecha());
          pst.setString(3,boletin.getPeriodo());
          pst.setInt(4,boletin.getEstudiante().getCodigo());

          pst.executeUpdate();
    }finally {
        if (pst != null) {
             pst.close();
        }
   }
}
public static boolean ActualizarBoletin (Boletin boletin, int cod) throws SQLException, Exception {
    mbd.conectar();
    con=mbd.getConexion();
    if ( consultarBoletin(cod) == null) {
           throw new SQLException ("Boletin no registrado" );
       }
       PreparedStatement pst = null;
       boolean res = false;
       try {
         pst = con.prepareStatement("Update Boletin set  codBoletin = ? , fecha = ?,  periodo = ?, codigoEstudiante=? where codBoletin = ?" ) ;
          pst.setInt(1,boletin.getCodigo());
          pst.setString(2,boletin.getFecha());
          pst.setString(3,boletin.getPeriodo());
          pst.setInt(4,boletin.getEstudiante().getCodigo());
          pst.setInt(5 , cod);
         int r = pst.executeUpdate();
       if (r != 0) {
          res = true;
       }
       } finally {
          if (pst != null) {
            pst.close();
          }
       }
         return res;
 }
public static boolean EliminarBoletin(int cod) throws SQLException, Exception {
    mbd.conectar();
    con=mbd.getConexion();
    if (consultarBoletin(cod) == null) {
        throw new SQLException("No hay Boletin para eliminar");
     }
     PreparedStatement pst = null;
     boolean res = false;
     try {
        pst = con.prepareStatement("delete from Boletin where codBoletin = ?");
        pst.setInt(1, cod);
        int r = pst.executeUpdate();
        if (r != 0) {
           res = true;
        }
     } finally {
        if (pst != null) {
           pst.close();
        }
     }
     return res;
}
  //metodo para listar asociados
public static List<Boletin> listarBoletin() throws SQLException, Exception {
      mbd.conectar();
        con=mbd.getConexion();
        ResultSet rs = null;
     PreparedStatement pst = null;
     List<Boletin> listaboletin = new LinkedList();
     try {
        pst = con.prepareStatement("select * from Boletin ");
        rs = pst.executeQuery();
        while(rs.next()) {
           listaboletin.add(Boletin.load(rs));
        }
    } finally {
        if (rs != null) {
           rs.close();
       }
        if (pst != null) {
          pst.close();
       }
  }
    return listaboletin;
}
}
